package com.whmall.service.crm.module.company.service.impl;


import com.whmall.component.entity.dto.PageReqDTO;
import com.whmall.component.entity.dto.PageRspDTO;
import com.whmall.component.service.AbstractDbService;
import com.whmall.service.crm.entity.dto.company.CompanyLogCreateReqDTO;
import com.whmall.service.crm.entity.dto.company.CompanyLogListReqDTO;
import com.whmall.service.crm.entity.dto.company.CompanyLogRspDTO;
import com.whmall.service.crm.module.company.dao.CompanyLogMapper;
import com.whmall.service.crm.module.company.entity.CompanyLogDO;
import com.whmall.service.crm.module.company.entity.qo.CompanyLogQO;
import com.whmall.service.crm.module.company.service.CompanyLogService;
import com.whmall.service.crm.util.PageDTOHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;


@Slf4j
@Service
public class CompanyLogServiceImpl extends AbstractDbService<CompanyLogMapper, CompanyLogDO> implements CompanyLogService {

    @Autowired
    private CompanyLogMapper companyLogMapper;

    @SuppressWarnings("unchecked")
    @Override
    public PageRspDTO<CompanyLogRspDTO> listCompanyLog(PageReqDTO pageReqDTO, CompanyLogListReqDTO reqDTO) {
        CompanyLogQO qo = new CompanyLogQO();
        BeanUtils.copyProperties(reqDTO, qo);
        PageDTOHelper.startPage(pageReqDTO);
        List<CompanyLogDO> logs = companyLogMapper.selectBy(qo);
        PageRspDTO pageRspDTO = PageDTOHelper.toPageDTO(logs);
        pageRspDTO.setRecords(
                logs.stream().map(e -> {
                    CompanyLogRspDTO rspDTO = new CompanyLogRspDTO();
                    BeanUtils.copyProperties(e, rspDTO);
                    return rspDTO;
                }).collect(Collectors.toList())
        );
        return pageRspDTO;
    }

    @Override
    public void createCompanyLog(CompanyLogCreateReqDTO createReqDTO) {
        CompanyLogDO logDO = new CompanyLogDO();
        BeanUtils.copyProperties(createReqDTO, logDO);
        companyLogMapper.insertSelective(logDO);
    }
}
